package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询所有地址
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有地址")
    public Result<List<AddressBook>> listAddressBook() {

        List<AddressBook> addressBooks = addressBookService.listAddressBook();

        return Result.success(addressBooks);
    }

    /**
     * 新增地址
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增地址")
    public Result<String> addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("[新增地址]{}", addressBook);

        addressBookService.addAddressBook(addressBook);

        return Result.success("新增地址成功");
    }

    /**
     * 根据id查询地址信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询地址信息")
    public Result<AddressBook> getAddressById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.selectAddressById(id);

        return Result.success(addressBook);
    }

    /**
     * 修改地址信息
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改地址信息")
    public Result<String> updateAddress(@RequestBody AddressBook addressBook) {
        log.info("[修改地址]{}", addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success("修改地址信息成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除地址信息")
    public Result<String> deleteAddress(Long id) {
        log.info("[删除地址]id:{}", id);

        addressBookService.deleteById(id);

        return Result.success("删除地址成功");
    }

    /**
     * 修改默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation(value = "修改默认地址")
    public Result<String> updateDefaultAddress(@RequestBody AddressBook addressBook) {
        log.info("[修改默认地址]id:{}", addressBook);

        addressBookService.updateDefaultAddress(addressBook);

        return Result.success("修改默认地址");
    }

    /**
     * 查看默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation(value = "查看默认地址")
    public Result<AddressBook> getDefaultAddre() {
        AddressBook addressBook = addressBookService.getDefaultAddress();

        return Result.success(addressBook);
    }
}
